<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Storage;
use DB;

class HealthAdvice extends Model
{
    protected $table = "health_advice";
    public $timestamps = false;

//是否热门
    const HOT_YES = 1;
    const HOT_NO = 0;
    static $HotDisplay = [
        self::HOT_NO => '非热门',
        self::HOT_YES => '热门',
    ];


    /**
     * 新增
     * @param $request
     * @return array
     */
    static function Publish($request)
    {

        DB::beginTransaction();

        //添加资讯
        $user = explode('||', $request->user);
        $advice = new HealthAdvice();
        $advice->title = $request->title;
        $advice->user_uuid = $user[0];
        $advice->user_name = $user[1];
        $advice->cover = $request->cover[0];
        $advice->audio_path = $request->audio_path;
        $advice->content = $request->contents;
        $advice->is_hot = $request->is_hot;
        $advice->type_value = $request->type_value;
        $advice->video_f = $request->video_f;
        $advice->audio_title = $request->audio_title;
        $advice->befrom = $request->befrom;
        $advice->type_name = DB::table('health_advice_type')->where('id', $request->type_value)->value('type');
        $advice->create_time = date('Y-m-d H:i:s');
        if (!$advice->save()) {
            DB::rollBack();

            return ['status' => 2, 'message' => '失败'];
        }
        //添加封面图
        $insert = [];
        $i = 0;
        foreach ($request->cover as $value) {
            $insert[] = ['health_id' => $advice->id, 'weight' => 3-$i, 'path' => $value];
        }
        if (!DB::table('health_pic')->insert($insert)) {
            DB::rollBack();

            return ['status' => 2, 'message' => '失败'];
        }



        DB::commit();

        return ['status' => 1, 'message' => '成功'];
    }


    /**
     * 修改
     * @param $advice
     * @param $request
     * @return array
     */
    static function Modify($advice, $request)
    {
        DB::beginTransaction();



        //修改封面
        if (!DB::table('health_pic')->where('health_id', $advice->id)->delete()) {
            DB::rollBack();

            return ['status' => 2, 'message' => '失败'];
        }
        $insert = [];
        $i = 0;
        foreach ($request->cover as $value) {
            $insert[] = ['health_id' => $advice->id, 'weight' => 3-$i, 'path' => $value];
        }
        if (!DB::table('health_pic')->insert($insert)) {
            DB::rollBack();

            return ['status' => 2, 'message' => '失败'];
        }
        //修改咨询
        $user = explode('||', $request->user);
        $advice->title = $request->title;
        $advice->user_uuid = $user[0];
        $advice->user_name = $user[1];
        $advice->cover = $value;
        $advice->audio_path = $request->audio_path;
        $advice->content = $request->contents;
        $advice->is_hot = $request->is_hot;
        $advice->type_value = $request->type_value;
        $advice->video_f = $request->video_f;
        $advice->audio_title = $request->audio_title;
        $advice->befrom = $request->befrom;
        $advice->type_name = DB::table('health_advice_type')->where('id', $request->type_value)->value('type');
        if (!$advice->save()) {
            DB::rollBack();

            return ['status' => 2, 'message' => '失败'];
        }

        DB::commit();

        return ['status' => 1, 'message' => '成功'];
    }

    /**
     * 添加视频
     * @param $request
     * @return array
     */
    static function VideoPublish($request)
    {
        $filename = Postings::OssVideo($request->video);
        if ($filename['status'] !== 1) {

            return $filename;
        }
        $path = Storage::url($filename['message']);
        $video_path = Storage::disk('public')->put('/',$request->video);
        //截取视频第一帧
        $pic_path = storage_path('app/public/test.png');
        $str = "ffmpeg -i " . storage_path('app/public/' . $video_path) . " -y -f mjpeg -ss 0 -t 1 " . $pic_path;

        system($str);//调用命令行
        //将截图上传到oss
        $pic = fopen($pic_path, 'r');
        Storage::disk('oss')->put($filename['message'] . '.jpg', $pic);
        //删除视频和截图
        Storage::disk('public')->delete('test.png');
        Storage::disk('public')->delete($video_path);

        $video = DB::table('health_advice_video')->insert([
            'title' => $request->title,
            'path' => $path,
            'pic_path' => $path.'.jpg',
        ]);
        if (!$video) {
            return ['status' => 2, 'message' => '失败'];
        }

        return ['status' => 1, 'message' => '成功'];
    }
}
